package org.wiztools.oembed;

import java.nio.charset.Charset;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author subhash
 */
class ContentTypeUtil {
    
    private static final Logger LOG = Logger.getLogger(ContentTypeUtil.class.getName());
    
    private static final Pattern RE_CHARSET = Pattern.compile(".*;\\s*charset=([^;]+).*");
    
    static Charset getCharsetFromContentTypeHeader(String contentType) {
        Matcher m = RE_CHARSET.matcher(contentType);
        if(m.matches()) {
            final String charsetStr = m.group(1);
            return Charset.forName(charsetStr);
        }
        LOG.warning("Content-type header does not have charset! Falling back to default: UTF-8.");
        return Charset.forName("UTF-8");
    }
}
